/*
 * copyright (c) 2009 Alexandr [Sn@ble] Surnin
 *
 * This file is part of FrameSeeker.
 *
 * FrameSeeker is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FrameSeeker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can read the full version of GNU LGPL in
 * http://www.gnu.org/copyleft/lesser.html
 */


#include "FSMethodHist.h"

// Static members declaration
quint64 FSMethodHist::thresholdLow;
quint64 FSMethodHist::thresholdHigh;


FSMethodHist::FSMethodHist() {

}


void FSMethodHist::process() {

//    qDebug() << "in process";
    prevHist.setImage(prev->getImage());
//    qDebug() << "before calc";
    prevHist.calculate();

    currentHist.setImage(current->getImage());
    currentHist.calculate();

    quint64 diff = calcHistDiff();

    if ((diff > thresholdLow) & (diff < thresholdHigh)) {
        //prev->saveImage(QString("P")+QString::number(diff, 10));
        current->saveImage(QString("c")+QString::number(diff, 10));
    }
}


quint64 FSMethodHist::calcHistDiff() {

    quint64 difference = 0;

    for (int i = 0; i < 256; i++) {
        difference += abs(currentHist.r(i) - prevHist.r(i));
        difference += abs(currentHist.g(i) - prevHist.g(i));
        difference += abs(currentHist.b(i) - prevHist.b(i));
    }

    qDebug() << "Difference == " << difference;
    return difference;
}


void FSMethodHist::readSettings(QSettings *settings) {


    FSMethodHist::thresholdLow = settings->value("Histograms_Method/Low_threshold").toInt();
    FSMethodHist::thresholdHigh = settings->value("Histograms_Method/High_threshold").toInt();

    qDebug() << "Histograms method:"
             << "\nLow threshold: " << thresholdLow
             << "\nHigh threshold: " << thresholdHigh
             << "\nTesting mode: " << (testingMode ? "true" : "false");
}
